//
//  OANewSMSViewController.m
//  OASystem
//
//  Created by zhoupengfei on 15/12/7.
//  Copyright © 2015年 zpf. All rights reserved.
//

#import "OANewSMSViewController.h"
#import "OAMainAddressViewController.h"
#import "Toast+UIView.h"
#import "HttpTools.h"
#import "OACityNameViewController.h"
#import "OANameListController.h"
#import "OASubNameListModel.h"
@interface OANewSMSViewController ()<OANameListControllerDelegate>

@end

@implementation OANewSMSViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.tableFooterView = [UIView new];
    self.title = @"新建";
    UIBarButtonItem * rightItem  = rightItem  = [[UIBarButtonItem alloc] initWithTitle:@"发送" style:UIBarButtonItemStylePlain target:self action:@selector(sendSMS)];;
    self.navigationItem.rightBarButtonItem = rightItem;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(getSelectName:) name:@"PostSelectNameNoti" object:nil];
    [self setupTextFieldRightView];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];

   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveNo:) name:@"11111" object:nil];


}

- (void)receiveNo:(NSNotification *)no{

     NSMutableArray *array = no.userInfo[@"text"];
    
     NSString *str = nil;

  for (NSString *str1 in array) {
    if (str.length == 0) {
   str  = [NSString stringWithFormat:@"%@",str1];

}else {
   str = [NSString stringWithFormat:@"%@,%@",str1,str];

}
    
    
    
  }
  self.receiveTextField.text = str;

}


-(void)getSelectName:(NSNotification*)note{
    
    NSArray * array = note.userInfo[@"SelectName"];
    self.receiveTextField.text = [array componentsJoinedByString:@","];
    
}
-(void)didSelecteName:(NSArray*)names{
    
    NSMutableArray * namesArray = [NSMutableArray array];
    for (OASubNameListModel * model in names) {
        [namesArray addObject:model.value];
    }
   
    self.receiveTextField.text = [namesArray componentsJoinedByString:@","];
    
}

-(void)sendSMS{

    if (self.contentsTextView.text.length == 0) {
        [self.view.window makeToast:@"输入短信内容"];
        return;
    }
    
    if (self.receiveTextField.text.length == 0 && self.phoneNumber.text.length == 0 ) {
        [self.view.window makeToast:@"请选择收件人或填写手机号"];
        return;
    }
    
    NSMutableDictionary * dict = [NSMutableDictionary dictionary];
    if (self.contentsTextView.text.length !=0) {
        [dict setObject:self.contentsTextView.text forKey:@"Contents"];
    }
    
    
        [dict setObject:self.receiveTextField.text forKey:@"Receives"];
    
    
    if (self.phoneNumber.text.length !=0) {
        [dict setObject:self.phoneNumber.text forKey:@"PhoneNumber"];
    }
    
    NSData * data  = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
    NSString * infoString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
   NSString * pass = [[NSUserDefaults standardUserDefaults]objectForKey:@"pwdId"];
NSArray * array =@[@{@"userId":userId},@{@"formxml":infoString}];
//    NSArray * array =@[@{@"userId":userId},@{@"pass":pass},@{@"formxml":infoString}];
      //  NSArray * array =@[@{@"userId":userId},@{@"content":self.contentsTextView.text },@{@"phone":self.phoneNumber.text },@{@"received":self.receiveTextField.text}];

    
    
   
    [HttpTools requestWithURL:KExchother arrays:array method:KMessageSend success:^(id JSON) {
        [self.view.window makeToast:@"提交短信成功"];
        [self.navigationController popViewControllerAnimated:YES];
    } failure:^(NSError *error) {
        [self.view.window makeToast:@"提交短信失败"];
    }];

    
   
}


#pragma mark 设置右边的view
-(void)setupTextFieldRightView{
    UIView * rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)];
    rightView.backgroundColor = [UIColor clearColor];
    UIButton * addNameBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [addNameBtn setImage:[UIImage imageNamed:@"addName.png"] forState:UIControlStateNormal];
    addNameBtn.frame = CGRectMake(0, 0, 40, 40);
    [rightView addSubview:addNameBtn];
    
    self.receiveTextField.rightView = addNameBtn;
    self.receiveTextField.rightViewMode=UITextFieldViewModeAlways;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.row == 1) {
        [self addNameAction];
    }
}

-(void)addNameAction{
//    [self keybordDown];
//    UIStoryboard * stroyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
//    OAMainAddressViewController * mainAddressVC = [stroyBoard instantiateViewControllerWithIdentifier:@"mainAddressVCID"];
//    mainAddressVC.addressType = LessMainAddressType;
//    [self.navigationController pushViewController:mainAddressVC animated:YES];
//     OACityNameViewController * nameVC = [[OACityNameViewController alloc]init];
//    __weak typeof(self) weakSelf = self;
//    nameVC.GetNameStringBlock = ^(NSString * str){
//        weakSelf.receiveTextField.text = str;
//    };
//    [self.navigationController pushViewController:nameVC animated:YES];
OANameListController * listVC = (OANameListController*)[self.storyboard instantiateViewControllerWithIdentifier:@"NameLIstID"];
    listVC.delegate = self;
    UINavigationController * nav = [[UINavigationController alloc]initWithRootViewController:listVC];
    [self.navigationController pushViewController:listVC animated:YES];
//    [self presentViewController:nav animated:YES completion:nil];



}

- (void)keybordDown {
    [self.contentsTextView resignFirstResponder];
    [self.receiveTextField resignFirstResponder];
    [self.phoneNumber resignFirstResponder];
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
